import 'dart:convert';
import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:flutter_reader/dao/bookinfo_data_manager.dart';
import 'package:flutter_reader/model/book/book_cata_list_model.dart';
import 'package:flutter_reader/model/book_cata_model.dart';
import 'package:flutter_reader/new/entity_novel_info.dart';
import 'package:flutter_reader/new/manager_router.dart';
import 'package:flutter_reader/pages/read_book/book_content.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:gzx_dropdown_menu/gzx_dropdown_menu.dart';

import 'book_introduction.dart';
import 'novel_book_reader_view.dart';

class CataSortCondition {
  String name;
  bool isSelected;

  CataSortCondition({this.name, this.isSelected});
}

class BookCatalogPage extends StatefulWidget {

  final String _bookId;
  final bool _hasCollect;

  final String bookName;
  final String bookImage;

  BookCatalogPage(this._bookId, this.bookName, this.bookImage,this._hasCollect);

  @override
  _BookCatalogPageState createState() {
    return _BookCatalogPageState();
  }
}

class _BookCatalogPageState extends State<BookCatalogPage> {

  List<Chapters> chaptersList = [];
  int nowPage = 1;

  GlobalKey<RefreshIndicatorState> _refreshIndicatorKey;
  ScrollController _scrollController;

  @override
  void initState() {
    // initPlatformState();
    _scrollController = new ScrollController();
    _refreshIndicatorKey = new GlobalKey<RefreshIndicatorState>();
    SchedulerBinding.instance.addPostFrameCallback((_){  _refreshIndicatorKey.currentState?.show(); } );
    this._onRefresh();
    _scrollController.addListener(() {
      if (_scrollController.position.pixels == _scrollController.position.maxScrollExtent) {
        _onLoadmore();
      }
    });
    super.initState();
  }

  Future<dynamic> _onRefresh() {
    chaptersList.clear();
    nowPage = 0;
    return BookDao.fetchBookCata(widget._bookId, nowPage).then((data) {
      setState(() => chaptersList.addAll(data.chapters));
    });
  }

  Future<dynamic> _onLoadmore() {
    nowPage++;
    return BookDao.fetchBookCata(widget._bookId, nowPage).then((data) {
      setState((){
        chaptersList.addAll(data.chapters);
      });
    });
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

  Widget _loadMoreWidget() {
    return new Padding(
      padding: const EdgeInsets.all(15.0),
      child: new Center(
          child: new CircularProgressIndicator()
      ),
    );
  }

  final items = List<String>.generate(35, (i) => '第$i章');
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
        appBar: AppBar(
          title: Text('章节目录'),
          backgroundColor: Color(0xffe53935),
        ),
        body: RefreshIndicator(
            onRefresh: _onRefresh,
            child: ListView.separated(
              controller: _scrollController,
              itemCount: chaptersList.length,
              itemBuilder: (context, index) {
                if (index == chaptersList.length - 1) {
                  return _loadMoreWidget();
                } else {
                  var _data = chaptersList[index];
                  return InkWell(
                    onTap: (){
                      NovelBookInfo currentBookInfo = NovelBookInfo()
                        ..bookId = widget._bookId
                        ..cover = widget.bookImage
                        ..title = widget.bookName
                        ..currentChapterIndex = index;

                      APPRouter.instance.route(
                          NovelBookReaderView.buildIntent(
                              context,
                              currentBookInfo));
                    },
                    child: ListTile(
                        title: Text(_data.title),
                        trailing: Icon(Icons.chevron_right)),
                  );
                }
              },
              separatorBuilder: (context, index) {
                return Divider();
              },
            )
        )
    );
  }
  
}
